// pages/userInfoCenter/index.js
const App = getApp();
const {Dialog} = require("../../../../../config/modal")
const utils = require("../../../../../utils/util")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        theamTextColor: App.globalData.theamTextColor,
        userInfo: {
            province: "请先授权",
            avatarUrl: '',
            school: '-',
            nickName: '-',
            inviteCode: '',
            integral: 99,
            usedIntegral: 10
        },
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        integral: {
            list: [],
            type: {
                IN: "IN",
                OUT: "OUT"
            }
        },
        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0,
        },
        searchPrams: "IN"
    },
    URL: {
        getList: "/integral/getList"
    },
    page: {
        progress: "/pages/tabbar/user/integral/progress/index"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
    },
    onShow() {
        this.initIntegral(this.data.page.pageNum, false);
    },

    // 提现进度 $navigateTo
    toProgress(e) {
        let item=e.currentTarget.dataset.item;
        if(item.source.indexOf('提现')>0||item.source==='CASH_OUT'){
            App.$navigateTo(this.page.progress+"?orderNo="+e.currentTarget.dataset.orderno, true)
        }
    },
    // 获取积分
    initIntegral(pageNum, loadMore) {
        let params = {
            pageNum: pageNum,
            pageSize: this.data.page.pageSize,
            type: this.data.searchPrams
        }
        this.setLoadingMore(loadMore, "loading", "加载中...")
        App.$get(!loadMore ? true : loadMore, this.URL.getList, params, res => {
            this.setData({
                "loadMore.show": false,
            })
            if (res.R) {
                this.setData({
                    "page.pageNum": res.data.pageNum,
                    "page.pageSize": res.data.pageSize,
                    "page.pages": res.data.pages
                })
                if (!loadMore) {
                    this.setData({
                        "integral.list": res.data.rows,
                    })
                    return;
                }
                let sourceData = this.data.integral.list;
                let newData = res.data.rows;
                newData.forEach(data => {
                    sourceData.push(data);
                })
                this.setData({
                    "integral.list":sourceData
                })
            }
        })
    },
    onClickTab(event) {
        let type = event.detail.name;
        this.setData({
            searchPrams: type
        })
        this.initIntegral(this.data.page.pageNum, false);
    },
    goBack() {
        wx.navigateBack();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.page.pages === 0 || this.data.page.pageNum + 1 > this.data.page.pages) {
            this.setLoadingMore(true, "end", "我是用底线的~")
            return;
        }
        this.initIntegral(this.data.page.pageNum + 1, true);
    },
    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
